﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace StatusPlan3
{
    public class StatusPlanHandler
    {
        DatabaseHandler dh;

        public StatusPlanHandler()
        {
            dh = new DatabaseHandler();   
        }

        internal Boolean login(string name, string password, Boolean winAuth = false)
        {
            return dh.login(name, password,winAuth);
        }

        public String getWindowsLogin()
        {
            return new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).Identity.Name;
        }

        //aaaaaaaaaaaa

        public ProjectGroup getProjectGroup(int id)
        {
            return dh.getProjectGroup(id);
        }

        public Roles getAllRoles()
        {
            return dh.getAllRoles();
        }

        public Roles getRoles(int user_id)
        {
            return dh.getUserRoles(user_id);
        }

        public Users getAllUsers()
        {
            return dh.getAllUsers();
        }

        public Companies getAllCompanies()
        {
            return dh.getAllCompanies();
        }

        public ProjectGroups getProjectGroups(int user_id,Boolean loadProjects = false)
        {
            return dh.getProjectGroups(user_id, loadProjects);
        }

        public Projects getProjects(int project_group_id)
        {
            return dh.getProjects(project_group_id);
        }

        public Subprojects getSubprojects(int project_id)
        {
            return dh.getSubprojects(project_id);
        }

        



        public Tasks getMyOpenTasks()
        {
            return dh.getMyOpenTasks();
        }

        public Issues getMyOpenIssues()
        {
            return dh.getMyOpenIssues();
        }




        internal Project saveProject(Project p, int project_group_id)
        {
            return dh.saveProject(p, project_group_id);;
        }

        internal void replyProjectMessage(Message message, Message m, Project project)
        {
            dh.replyMessage(message, m);
        }

        internal void replyTaskMessage(Message message, Message m, Task task)
        {
            dh.replyMessage(message, m);
        }

        internal void replyIssueMessage(Message message, Message m, Issue issue)
        {
            dh.replyMessage(message, m);
        }

        internal void replySubprojectMessage(Message message, Message m, Subproject subproject)
        {
            dh.replyMessage(message, m);
        }

        internal void newProjectMessage(Message message, Project project)
        {
            dh.saveProjectMessage(message, project);
        }

        internal void newTaskMessage(Message message, Task task)
        {
            dh.saveTaskMessage(message, task);
        }

        internal void newIssueMessage(Message message, Issue issue)
        {
            dh.saveIssueMessage(message, issue);
        }

        internal void newSubprojectMessage(Message message, Subproject subproject)
        {
            dh.saveSubprojectMessage(message, subproject);
        }

        internal Task getTask(int id)
        {
            return dh.getTask(id);
        }

        internal void openFile(AttachedFile af)
        {
            dh.openFile(af);
        }

        internal void downloadFile(AttachedFile af)
        {
            dh.downloadFile(af);
        }

        internal void addTaskFile(AttachedFiles afs, Task task)
        {
            String error = dh.saveTaskFile(afs, task);
            if (error.Length > 0)
            {
                MessageBox.Show("Kundi ikki innlesa fílur.:\n" + error, "Feilur: Innlesing", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public AttachedFiles buildFilesFromString(string[] paths, AttachedFile pFolder = null)
        {
            AttachedFiles afs = new AttachedFiles();

            foreach (String file in paths)
            {
                String pathToFile = file;
                String fileName = file.Substring(file.LastIndexOf("\\") + 1);
                System.IO.FileInfo f = new System.IO.FileInfo(pathToFile);

                if (!System.IO.Directory.Exists(pathToFile))
                {
                    String fileType = fileName.Substring(fileName.LastIndexOf('.') + 1);
                    fileName = fileName.Replace("." + fileType, "");
                    

                    AttachedFile af = new AttachedFile()
                    {
                        createdBy = StatusPlanData.currentUser,
                        dateAdded = DateTime.Now,
                        fileName = fileName,
                        fileType = fileType,
                        size = f.Length,
                        path = pathToFile,
                    };

                    if (pFolder != null)
                        af.folder = pFolder;

                    afs.Add(af);
                }
                //IsFolder
                else
                {
                    if (System.IO.Directory.Exists(pathToFile))
                    {
                        AttachedFile af = new AttachedFile()
                        {
                            createdBy = StatusPlanData.currentUser,
                            dateAdded = DateTime.Now,
                            fileName = fileName,
                            fileType = "folder",
                            size = 0,
                            path = pathToFile,
                            isFolder = true,
                        };

                        string[] files2 = System.IO.Directory.GetFiles(pathToFile);
                        string[] directories = System.IO.Directory.GetDirectories(pathToFile);

                        foreach (AttachedFile af2 in buildFilesFromString(files2, af))
                        {
                            af.attachedFiles.Add(af2);
                        }

                        foreach (AttachedFile af2 in buildFilesFromString(directories, af))
                        {
                            af.attachedFiles.Add(af2);
                        }

                        afs.Add(af);
                    }
                }
                
            }

            return afs;
        }

        internal void removeTaskFile(AttachedFile af, Task task)
        {
            dh.deleteTaskFile(af, task);
        }

        public void taskDownloadAllFiles(Task t)
        {
            dh.taskDownloadAllFiles(t);
        }

        internal Task saveTask(Task task, ProjectGroup projectGroup, Project project, Subproject subproject)
        {
            return dh.saveTask(task, projectGroup, project, subproject);
        }

        internal void saveType(Type t, ProjectGroup projectGroup)
        {
            dh.saveType(t, projectGroup);
        }

        internal void deleteTask(ProjectGroup projectGroup, Type t)
        {
            dh.deleteType(projectGroup, t);
        }

        internal void saveIssueType(IssueType orgType, ProjectGroup projectGroup)
        {
            dh.saveIssueType(orgType, projectGroup);
        }

        internal void deleteIssueType(ProjectGroup projectGroup, IssueType t)
        {
            dh.deleteIssueType(projectGroup, t);
        }

        internal void saveState(Status orgStatus, ProjectGroup projectGroup)
        {
            dh.saveState(projectGroup, orgStatus);
        }

        public void deleteState(Status orgStatus, ProjectGroup projectGroup)
        {
            dh.deleteStatus(projectGroup, orgStatus);
        }

        internal void saveIssueState(IssueStatus t, ProjectGroup projectGroup)
        {
            dh.saveIssueState(t, projectGroup);
        }

        internal void deleteIssueState(IssueStatus t, ProjectGroup projectGroup)
        {
            dh.deleteIssueStatus(projectGroup, t);
        }

        internal void savePriority(Priority t, ProjectGroup projectGroup)
        {
            dh.savePriority(t, projectGroup);
        }

        internal void deletePriority(Priority t, ProjectGroup projectGroup)
        {
            dh.deletePriority(projectGroup, t);
        }

        internal void addProjectGroupMembers(Users usersToAdd, ProjectGroup pg)
        {
            dh.saveProjectGroupMembers(usersToAdd, pg);
        }

        internal ProjectGroups getAllProjectGroups()
        {
            return dh.getAllProjectGroups();
        }

        internal void saveUser(User user)
        {
            dh.saveUser(user);
        }

        internal void deleteUser(User u)
        {
            dh.deleteUser(u);
        }

        internal void saveProjectGroup(ProjectGroup projectGroup)
        {
            dh.saveProjectGroup(projectGroup);
        }

        internal void removeProjectFile(AttachedFile af, Project project)
        {
            dh.deleteProjectFile(af,project);
        }

        internal void addProjectFile(AttachedFiles afs, Project project)
        {
            String error = dh.saveProjectFile(afs, project);
            if (error.Length > 0)
            {
                MessageBox.Show("Kundi ikki innlesa fílur.:\n" + error, "Feilur: Innlesing", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        internal void projectDownloadAllFiles(Project project)
        {
            dh.projectDownloadAllFiles(project);
        }

        internal Project getProject(int p)
        {
            return dh.getProject(p);
        }

        internal void setAsStandard(ProjectGroup pg, Project p)
        {
            dh.setAsStandard(pg, p);
        }
    }

    public static class StatusPlanData
    {
        public static StatusPlanHandler statusPlanHandler { get; set; }
        public static ProjectGroups projectGroups { get; set; }
        public static Users users { get; set; }
        public static Roles roles { get; set; }
        public static Companies companies { get; set; }
        public static User currentUser { get; set; }
        public static MainWindow mainWindow { get; set; }

        private static readonly Random _rng = new Random();
        private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        public static string getRandomString(int size) { char[] buffer = new char[size]; for (int i = 0; i < size; i++) buffer[i] = _chars[_rng.Next(_chars.Length)]; return new string(buffer); }
    }
}
